﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using VISS.API.Admins;
using System.Web.SessionState;
using NCT.Framework;


public enum EMenuIndex {
    Home = 1,
    User = 2,
    Commons = 4
}

public enum ESubMenuIndex {
    
    None,

    /*User*/
    General,
    UserList,
    Search,
    Mesasge,
    Report,
    Mail,

    /*Common*/
    Attraction,
    Body,
    City,
    Country,
    Driking,
    Education,
    Ethnic,
    HairStyle,
    House,
    Job,
    Language,
    LivingStatus,
    LunarYear,
    MaritalStatus,
    Moving,
    Nationality,
    Pet,
    Religion,
    Smoking,
    ZodiacMonth
}

public enum ECommonMenuIndex {
    
    
}

/// <summary>
/// Summary description for AppSettings
/// </summary>
public class AppSettings {

    public static string MailType = ConfigUtils.GetValue("viss.mail.type", "frontend.viss.vn");

    public static string Title = "NCT - VISS Admin Tools";

    public static string RootURL = GetApplicationUrl();
    public static string ResouresURL = RootURL + "res/";
    public static string ImagesURL = ResouresURL + "images/";
    public static string UserURL = RootURL + "users/";
    public static string CommonURL = RootURL + "commons/";

    //Menu
    public static EMenuIndex MenuIndex;
    public static ESubMenuIndex SubMenuIndex;
   
    //Session
    public const string SESSION_ADMIN_LOGIN = "VISS.Admin.UserLogin";

    //Ftp Upload
    public static readonly string FtpUploadServer = ConfigUtils.GetValue("ftp.server");
    public static readonly string FtpUploaFolder = ConfigUtils.GetValue("ftp.folder");
    public static readonly string FtpUploadUser = ConfigUtils.GetValue("ftp.username");
    public static readonly string FtpUploadPassword = ConfigUtils.GetValue("ftp.password");

    //Image Store
    public static readonly string ImageStoreUploadServer = ConfigUtils.GetValue("http.server.upload", "http://viss.vn/res/images/users/");

    //Paging
    public static readonly int RecordPerPage = ConfigUtils.GetIntValue("VISS.Admin.Paging", 20);
    public static readonly int RecordPerPageOfMessage = ConfigUtils.GetIntValue("VISS.Admin.Paging.Message", 50);

    public AppSettings() {
        //
        // TODO: Add constructor logic here
        //
    }

    private static string GetApplicationUrl() {
        HttpContext ctx = HttpContext.Current;

        string toReturn = (string)ctx.Cache.Get("ApplicationUrl");
        if (String.IsNullOrEmpty(toReturn)) {
            string protocol = ctx.Request.ServerVariables.Get("SERVER_PORT_SECURE");
            string port = ctx.Request.ServerVariables.Get("SERVER_PORT");
            string host = ctx.Request.ServerVariables.Get("SERVER_NAME");

            if (protocol.Equals("off", StringComparison.OrdinalIgnoreCase) || protocol.Equals("0"))
                protocol = "http://";
            else
                protocol = "https://";

            if (port.Equals("80"))
                port = String.Empty;
            else
                port = ":" + port;

            if (!String.IsNullOrEmpty(ctx.Request.ApplicationPath) && ctx.Request.ApplicationPath != "/")
                toReturn = String.Format("{0}{1}{2}{3}/", protocol, host, port, ctx.Request.ApplicationPath);
            else
                toReturn = String.Format("{0}{1}{2}/", protocol, host, port);
        }

        return toReturn;
    }

    public static bool CheckRight(HttpSessionState session, EAdminRight right) {
        try {
            AdminEnt uItem = GetAdminLogin(session);
            if (uItem != null) {
                return ((uItem.Rights & right) > 0);
            }
        }
        catch { }
        return false;
    }

    public static void SetMenuIndex(string path) {
        if (path.Contains("users")) {
            AppSettings.MenuIndex = EMenuIndex.User;

            if (path.Contains("general")) {
                SubMenuIndex = ESubMenuIndex.General;
            }
            else if (path.Contains("userList")) {
                SubMenuIndex = ESubMenuIndex.UserList;
            }
            else if (path.Contains("search")) {
                SubMenuIndex = ESubMenuIndex.Search;
            }
            else if (path.Contains("userMessage")) {
                SubMenuIndex = ESubMenuIndex.Mesasge;
            }
            else if (path.Contains("userReport")) {
                SubMenuIndex = ESubMenuIndex.Report;
            }
            else if (path.Contains("mail")) {
                SubMenuIndex = ESubMenuIndex.Mail;
            }
            else {
                SubMenuIndex = ESubMenuIndex.None;
            }
        }
        else if (path.Contains("commons")) {
            AppSettings.MenuIndex = EMenuIndex.Commons;

            if (path.Contains("general")) {
                SubMenuIndex = ESubMenuIndex.General;
            }
            else if (path.Contains("attraction")) {
                SubMenuIndex = ESubMenuIndex.Attraction;
            }
            else if (path.Contains("body")) {
                SubMenuIndex = ESubMenuIndex.Body;
            }
            else if (path.Contains("city")) {
                SubMenuIndex = ESubMenuIndex.City;
            }
            else if (path.Contains("country")) {
                SubMenuIndex = ESubMenuIndex.Country;
            }
            else if (path.Contains("drinking")) {
                SubMenuIndex = ESubMenuIndex.Driking;
            }
            else if (path.Contains("education")) {
                SubMenuIndex = ESubMenuIndex.Education;
            }
            else if (path.Contains("ethnic")) {
                SubMenuIndex = ESubMenuIndex.Ethnic;
            }
            else if (path.Contains("hairstyle")) {
                SubMenuIndex = ESubMenuIndex.HairStyle;
            }
            else if (path.Contains("house")) {
                SubMenuIndex = ESubMenuIndex.House;
            }
            else if (path.Contains("job")) {
                SubMenuIndex = ESubMenuIndex.Job;
            }
            else if (path.Contains("language")) {
                SubMenuIndex = ESubMenuIndex.Language;
            }
            else if (path.Contains("livingstatus")) {
                SubMenuIndex = ESubMenuIndex.LivingStatus;
            }
            else if (path.Contains("lunaryear")) {
                SubMenuIndex = ESubMenuIndex.LunarYear;
            }
            else if (path.Contains("maritalstatus")) {
                SubMenuIndex = ESubMenuIndex.MaritalStatus;
            }
            else if (path.Contains("moving")) {
                SubMenuIndex = ESubMenuIndex.Moving;
            }
            else if (path.Contains("nationality")) {
                SubMenuIndex = ESubMenuIndex.Nationality;
            }
            else if (path.Contains("pet")) {
                SubMenuIndex = ESubMenuIndex.Pet;
            }
            else if (path.Contains("religion")) {
                SubMenuIndex = ESubMenuIndex.Religion;
            }
            else if (path.Contains("smoking")) {
                SubMenuIndex = ESubMenuIndex.Smoking;
            }
            else if (path.Contains("zodiacmonth")) {
                SubMenuIndex = ESubMenuIndex.ZodiacMonth;
            }
        }
        else {
            AppSettings.MenuIndex = EMenuIndex.Home;
        }
    }

    public static AdminEnt GetAdminLogin(HttpSessionState session) {
        try {
            return (AdminEnt)session[SESSION_ADMIN_LOGIN];
        }
        catch { }
        return null;
    }

    public static void SetAdminLogin(HttpSessionState session, AdminEnt item) {
        session[SESSION_ADMIN_LOGIN] = item;
    }

    public static void ResetAdminLogin(HttpSessionState session) {
        session[SESSION_ADMIN_LOGIN] = null;
    }
}


